<?php

namespace App\Controller\Admin;

use App\Controller\Example\FundsExample;
use App\Models\Conn;
use Core\Common\Common;
use Core\Result\Result;

class Funds
{
    /**
     * 管理后台明细列表
     * entry token,uid,username,truename,type,number,genre,startTime,endTime,page,limit
     * return json
     * */
    public function fundsList($data){
        $username = $data["username"];
        $truename = $data["truename"];
        $assemble = Common::assemble(new FundsExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $joinSql = " WHERE 1 ";
        if($assemble->uid !=null){ $joinSql.=" and `uid` ='$assemble->uid' ";}
        if($username !=null){ $joinSql.=" and u.`username` ='$username' ";}
        if($truename !=null){ $joinSql.=" and u.`truename` ='$truename' ";}
        if($assemble->type !=null){ $joinSql.=" and `type` ='$assemble->type' ";}
        if($assemble->number !=null){ $joinSql.=" and `number` ='$assemble->number' ";}
        if($assemble->genre !=null){ $joinSql.=" and `genre` ='$assemble->genre' ";}
        if($assemble->startTime !=null){$assemble->startTime = strtotime($assemble->startTime); $joinSql.=" and UNIX_TIMESTAMP(`time`) >= '$assemble->startTime' ";}
        if($assemble->endTime !=null){$assemble->endTime = strtotime($assemble->endTime); $joinSql.=" and UNIX_TIMESTAMP(`time`) <= '$assemble->endTime' ";}
        $userSql = "SELECT f.`id`,`uid`,`username`,`truename`,`number`, `type`, `amount`, `balance`, `time`,`notes`,`genre`,`fund_status` FROM `funds` as f inner join `users` as u on f.uid = u.id $joinSql order by id desc limit $identical->pageStart,$identical->limit";
        $readConn = $conn->readConn($userSql,true);
        foreach ($readConn as $item=>$value){
            switch ($value["genre"]){
                //1余额2消费积分3创业积分4电池5现金
                case 1:$readConn[$item]["genre"]="通用余额";break;
                case 2:$readConn[$item]["genre"]="专属余额";break;
                case 5:$readConn[$item]["genre"]="现金";break;
                default :$readConn[$item]["genre"]="未知";break;
            }
        }
        $returnData["list"]=$readConn;
        $userCountSql="SELECT count(f.`id`) as count FROM `funds` as f inner join `users` as u on f.uid = u.id $joinSql";
        $totalSql="SELECT sum(amount) as 总交易数量 FROM `funds` as f inner join `users` as u on f.uid = u.id $joinSql limit $identical->pageStart,$identical->limit";
        $returnData["count"] = $conn->readConn($userCountSql)["count"];
        $returnData["total"] = $conn->readConn($totalSql);
        Result::success($returnData);
    }
}